package com.suoluo.zenadmin.common;

import java.math.BigDecimal;


public final class GeometryUtil {
	
	public static interface LatLng{
		BigDecimal getLat();
		BigDecimal getLng();
	}
	private GeometryUtil(){
	}
	
private static double EARTH_R = 6378.137;
	
	public static double degreesToRadians(double deg){
		return deg * (Math.PI / 180);
	}
	
	
	
	public static double radiansToDegrees(double rad){
		return rad / (Math.PI / 180);
	}
	
	public static double calcDistance(double lat1,double lng1,double lat2,double lng2){
		double f = degreesToRadians((lat1 + lat2) /2);
		double g = degreesToRadians((lat1 - lat2)/2);
		double l = degreesToRadians((lng1 - lng2)/2);
		double sg = Math.sin(g);
		double sl = Math.sin(l);
		double sf = Math.sin(f);
		
		double s,c,w,r,d,h1,h2;
		double a = EARTH_R;
		double fl = 1/298.257;
		sg = sg*sg;
		sl = sl*sl;
		sf = sf*sf;

		s = sg*(1-sl) + (1-sf)*sl;
		c = (1-sg)*(1-sl) + sf*sl;

		w = Math.atan(Math.sqrt(s/c));
		r = Math.sqrt(s*c)/w;
		d = 2*w*a;
		h1 = (3*r -1)/2/c;
		h2 = (3*r +1)/2/s;
		
		double value =  d*(1 + fl*(h1*sf*(1-sg) - h2*(1-sf)*sg));
		//四舍五入，保留3位精度
		return round(value,3);
	}
	
	public static double round(double v, double p) {
		double t = 1.0;
		for (; p > 0; t *= 10, p--);
		for (; p < 0; t /= 10, p++);
		return Math.round(v * t) / t;
	}
	
	public static double calcDistance(LatLng from,LatLng to){
		if (from == null || to == null){
			return 0;
		}
		BigDecimal fromLat = from.getLat();
		BigDecimal fromLng = from.getLng();
		BigDecimal toLat = to.getLat();
		BigDecimal toLng = to.getLng();
		if (fromLat == null || fromLng == null || toLat == null || toLng == null){
			return 0;
		}
		return calcDistance(fromLat.doubleValue(), fromLng.doubleValue(), toLat.doubleValue(), toLng.doubleValue());
	}
	
	public static TrafficEnum selectTraffic(double distance){
		
//		double distance = calcDistance(from, to);
		for (TrafficEnum tenum : TrafficEnum.values()){
			if (tenum.from <= distance && distance < tenum.to ){
				return tenum;
			}
		}
		return null;
	}
	
	
	public static enum TrafficEnum {

		//	trafficTypes:[{from:0,to:0.5,speed:0.05,icon:"/resources/portal/img/trip/walk.png"},
		//    {from:0.5,to:500,speed:1,icon:"/resources/portal/img/trip/car.png"},
		//	  {from:500,to:9999999999,speed:12,icon:"/resources/portal/img/trip/plane.png"}]};
		WORK(0,0.5,0.05,"/resources/portal/img/trip/walk.png"){
		},
		CAR(0.5,500,1,"/resources/portal/img/trip/car.png"){
		},
		PLANE(500,999999999,12,"/resources/portal/img/trip/plane.png"){
		};

		private double from;
		private double to;
		private double speed;

		private String icon;


		private TrafficEnum(double from,double to,double speed,String icon){
			this.from = from;
			this.to = to;
			this.speed = speed;
			this.icon = icon;
		}

		public int calcTime(double distance){
			return  (int) Math.ceil(distance / this.speed);
		}

		public String getIcon(){
			return this.icon;
		}
	}
}
